package com.rong.mbt.personal.activity;


import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.calanger.hh.entity.ResultView;
import com.calanger.hh.model.Doctor;
import com.calanger.hh.model.Hospital;
import com.calanger.hh.model.Offices;
import com.calanger.hh.model.User;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.rong.mbt.personal.R;
import com.rong.mbt.personal.R2;
import com.rong.mbt.personal.activity.base.PBaseActivity;
import com.rong.mbt.personal.http.UrlApi;
import com.rong.mbt.personal.http.callback.JsonCallback;
import com.rong.mbt.personal.utils.SpUtils;
import com.shehuan.niv.NiceImageView;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DoctorInfomationActivity extends PBaseActivity {

    protected Context mContext;

    //findView
    @BindView(R2.id.header_center_text)
    TextView headerCenterText;
    @BindView(R2.id.right_ll_text)
    TextView rightLlText;
    @BindView(R2.id.right_ll)
    LinearLayout rightLl;
    @BindView(R2.id.et_heartMessage)
    TextView etHeartMessage;
    @BindView(R2.id.et_achieveReward)
    EditText etAchieveReward;
    @BindView(R2.id.et_eduBackgroup)
    EditText etEduBackgroup;
    @BindView(R2.id.et_summary)
    EditText etSummary;
    @BindView(R2.id.tv_doctor_level_value)
    TextView tvDoctorLevelValue;
    @BindView(R2.id.tv_Consultation)
    TextView tvConsultation;
    @BindView(R2.id.iv_doctor_head)
    NiceImageView ivDoctorHead;
    @BindView(R2.id.tv_doctor_name)
    TextView tvDoctorName;
    @BindView(R2.id.tv_doctor_type)
    TextView tvDoctorType;
    @BindView(R2.id.tv_doctor_offices)
    TextView tvDoctorOffices;
    @BindView(R2.id.tv_doctor_hospital)
    TextView tvDoctorHospital;
    @BindView(R2.id.rl_doctor_detail_head)
    RelativeLayout rlDoctorDetailHead;
    @BindView(R2.id.ll_doctor_level)
    LinearLayout llDoctorLevel;
    @BindView(R2.id.ll_doctor_offices)
    LinearLayout llDoctorOffices;
    @BindView(R2.id.ll_doctor_hospital)
    LinearLayout llDoctorHospital;
    @BindView(R2.id.ll_certified_img_group)
    LinearLayout llCertifiedImgGroup;
    @BindView(R2.id.bt_doctor_submit)
    Button btDoctorSubmit;

    //intent
    private int doctorId;

    //check
    private int doctorTitle;
    private int officesId;
    private int hospitalId;
    private String summary;
    private String eduBackgroup;
    private String achieveReward;
    private String heartMessage;

    //initDate
    private Doctor doctor;
    private User user;
    private Hospital hospital;
    private Offices offices;

    @Override
    public int getLayoutId() {
        return R.layout.activity_doctor_infomation;
    }

    @Override
    public void initView() {
        super.initView();
        mContext = this;
//intent 传递参数处理  ---- 后期改成用Rount
        doctorId = getIntent().getIntExtra("doctorId", 0);
        if (doctorId != 0) {
            rightLlText.setText("编辑");
            rightLl.setVisibility(View.VISIBLE);
            tvConsultation.setVisibility(View.GONE);
        }
//初始化数据
        findView();
        initData();
        listener();

    }

    @OnClick(R2.id.right_ll)
    public void toEdit() {
        if ("编辑".equals(rightLlText.getText().toString())) {
            rlDoctorDetailHead.setVisibility(View.GONE);
            llCertifiedImgGroup.setVisibility(View.GONE);
            llDoctorLevel.setVisibility(View.VISIBLE);
            llDoctorOffices.setVisibility(View.VISIBLE);
            llDoctorHospital.setVisibility(View.VISIBLE);
            btDoctorSubmit.setVisibility(View.VISIBLE);
            etSummary.setEnabled(true);
            etEduBackgroup.setEnabled(true);
            etAchieveReward.setEnabled(true);
            etHeartMessage.setEnabled(true);

            rightLlText.setText("取消");
        } else {
            rlDoctorDetailHead.setVisibility(View.VISIBLE);
            llCertifiedImgGroup.setVisibility(View.GONE);
            llDoctorLevel.setVisibility(View.GONE);
            llDoctorOffices.setVisibility(View.GONE);
            llDoctorHospital.setVisibility(View.GONE);
            btDoctorSubmit.setVisibility(View.GONE);
            etSummary.setEnabled(false);
            etEduBackgroup.setEnabled(false);
            etAchieveReward.setEnabled(false);
            etHeartMessage.setEnabled(false);
            rightLlText.setText("编辑");
        }

    }


    public void checkOk() {
//        doctorTitle = tvDoctorLevelValue.getText().toString();
        summary = etSummary.getText().toString();
        eduBackgroup = etEduBackgroup.getText().toString();
        achieveReward = etAchieveReward.getText().toString();
        heartMessage = etHeartMessage.getText().toString();

    }

    @OnClick(R2.id.tv_doctor_level_value)
    public void onViewClicked() {


    }

    @SuppressLint("ResourceType")
    private void findView() {
        TextView tvTitle = findViewById(R.id.header_center_text);
        tvTitle.setText("医生详情");

        rlDoctorDetailHead.setVisibility(View.VISIBLE);
        llCertifiedImgGroup.setVisibility(View.GONE);
        llDoctorLevel.setVisibility(View.GONE);
        llDoctorOffices.setVisibility(View.GONE);
        llDoctorHospital.setVisibility(View.GONE);
        btDoctorSubmit.setVisibility(View.GONE);
        etSummary.setEnabled(false);
        etEduBackgroup.setEnabled(false);
        etAchieveReward.setEnabled(false);
        etHeartMessage.setEnabled(false);
        rightLlText.setText("编辑");

    }

    private void initData() {
        OkGo.<ResultView>post(UrlApi.doctorGetSingle)
                .tag(this)
                .headers("mbtToken", SpUtils.getToken(getApplicationContext()))
                .params("id", doctorId)
                .execute(new JsonCallback<ResultView>() {
                    @Override
                    public void onSuccess(Response<ResultView> response) {
                        Log.i("test", "onSuccess  " + response.body());
                        ResultView resultView = response.body();
                        if (resultView.getResultCode() == 0) {
                            Map<String, Object> map = (Map<String, Object>) resultView.getData();
                            JSONObject jo = new JSONObject(map);
                            doctor = jo.getObject("doctor", Doctor.class);
                            user = (User) jo.getObject("user", User.class);
                            hospital = (Hospital) jo.getObject("hospital", Hospital.class);
                            offices = jo.getObject("offices", Offices.class);
                            Glide.with(mContext).load(doctor.getHeaderimg()).into(ivDoctorHead);
                            tvDoctorName.setText(doctor.getName());
                            tvDoctorLevelValue.setText(doctor.getDoctorTitle() == 1 ? "助理医生" : "执业医师");
                            tvDoctorOffices.setText(offices.getName());
                            tvDoctorHospital.setText(hospital.getName());

                            etSummary.setText(doctor.getSummary());
                            etEduBackgroup.setText(doctor.getEduBackgroup());
                            etAchieveReward.setText(doctor.getAchieveReward());
                            etHeartMessage.setText(doctor.getHeartMessage());

                        } else {
                            Toast.makeText(DoctorInfomationActivity.this, resultView.getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Response<ResultView> response) {
                        super.onError(response);
                        Log.i("test", "onError  " + response.message());
                        Toast.makeText(DoctorInfomationActivity.this, response.message(), Toast.LENGTH_SHORT).show();
                    }

                });


    }

    private void listener() {
        findViewById(R.id.ll_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        findViewById(R.id.bt_doctor_submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkOk();
                OkGo.<ResultView>post(UrlApi.doctorEdit)
                        .tag(this)
                        .headers("mbtToken", SpUtils.getToken(getApplicationContext()))
                        .params("id", doctorId)
                        .params("doctorTitle", doctorTitle)
                        .params("officesId", officesId)
                        .params("hospitalId", hospitalId)
                        .params("summary", summary)
                        .params("eduBackgroup", eduBackgroup)
                        .params("achieveReward", achieveReward)
                        .params("heartMessage", heartMessage)
                        .execute(new JsonCallback<ResultView>() {
                            @Override
                            public void onSuccess(Response<ResultView> response) {
                                Log.i("test", "onSuccess  " + response.body());
                                ResultView resultView = response.body();
                                if (resultView.getResultCode() == 0) {
                                    Toast.makeText(DoctorInfomationActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                } else {
                                    Toast.makeText(DoctorInfomationActivity.this, resultView.getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Response<ResultView> response) {
                                super.onError(response);
                                Log.i("test", "onError  " + response.message());
                                Toast.makeText(DoctorInfomationActivity.this, response.message(), Toast.LENGTH_SHORT).show();
                            }

                        });
            }
        });
    }
}
